home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Internet / News / Alexandra.0.82 / Source / FaceView.m < prev    next >
Encoding:
Text File  |  1996-02-06  |  4.0 KB  |  165 lines

  1.  
  2. #import "FaceView.h"
  3. #import <c.h>
  4. #import <ctype.h>
  5.  
  6. @implementation FaceView
  7.  
  8. //-----------------------------------------------------------------------
  9. // PRIVATE METHODS
  10. //-----------------------------------------------------------------------
  11.  
  12. // In Ermangelung der passenden RFC eine einfache Heuristik. Vom '@' ausgehend
  13. // wird der Anfang und das Ende des Logins gesucht.
  14. // Fuer eine "richtige" Loesung: Was gibt's fuer Formate?
  15. //   1) login 
  16. //   2) login (real name)
  17. //   3) "real name" <login>
  18. //   4) real name <login>
  19. //   ...
  20.  
  21. #define islogin(c) (isalnum(c) || (c)=='.' || (c)=='_' || (c)=='%' || (c)=='-')
  22.  
  23. - (char *)loginForName:(const char *)userName;
  24.     {
  25.     char        login[300],*at,*p;
  26.     int            n;
  27.     
  28.     at=strchr(userName,'@');
  29.     if(at)
  30.         {
  31.         n=1;
  32.         for(p=at+1;islogin(*p);p++)
  33.             n++;
  34.         for(p=at-1;p>=userName && islogin(*p);p--)
  35.             n++;
  36.         strncpy(login,p+1,MIN(n,300));
  37.         login[MIN(n,300)]='\0';
  38.         for(p=login;*p;p++)
  39.             if(*p=='!' || *p=='@')
  40.                 *p='.';
  41.             else
  42.                 *p=NXToLower(*p);
  43.         at=NXCopyStringBufferFromZone(login,[self zone]);
  44.         }
  45.     return at;
  46.     }
  47.  
  48.  
  49. // NewsGrazer Methode. Mail ist cleverer und sieht noch in der Aliases-Datei nach
  50. // und testet auf lokalen Benutzer...
  51.  
  52. - (char *)filenameForLogin:(const char *)login
  53.     {
  54.     char buf[MAXPATHLEN+1];
  55.     
  56.     sprintf(buf,"/LocalLibrary/Images/People/%s.tiff",login);
  57.     return NXCopyStringBufferFromZone(buf,[self zone]);    
  58.     }
  59.  
  60.  
  61. //-----------------------------------------------------------------------
  62. // INSTANCE VAR
  63. //-----------------------------------------------------------------------
  64.  
  65. - setImage:(NXImage *)anImage;
  66.     {
  67.     myImage=anImage;
  68.     return self;
  69.     }
  70.  
  71.  
  72. - (NXImage *)image;
  73.     {
  74.     return myImage;
  75.     }
  76.  
  77.  
  78. //-----------------------------------------------------------------------
  79. // SET IMAGE FROM LOGIN NAME
  80. //-----------------------------------------------------------------------
  81. - (BOOL)showFaceForName:(const char *)name;
  82.     {
  83.     NXImage        *image=nil;
  84.     char        *imageFile,*login;
  85.  
  86.     if(name && (login=[self loginForName:name])!=NULL)
  87.         {
  88.         image=[NXImage findImageNamed:login];
  89.         if(!image)
  90.             {
  91.             imageFile=[self filenameForLogin:login];
  92.             image=[[NXImage allocFromZone:[self zone]] initFromFile:imageFile];
  93.             NXZoneFree([self zone],imageFile);
  94.             if(image && [image lockFocus])
  95.                 {
  96.                 NXSize size, newsize={64,64};
  97.  
  98.                 [image unlockFocus];
  99.                 // [TRH] Don't scale image if it isn't necessary,
  100.                 // and keep size/width proportions intact if it is.
  101.                 [image getSize:&size];
  102.                 if (size.height > newsize.height || size.width > newsize.width)
  103.                     {
  104.                     if (size.height > size.width)
  105.                         newsize.width=newsize.width*size.width/size.height;
  106.                     else if (size.height < size.width)
  107.                         newsize.height = newsize.height*size.height/size.width;
  108.                     [image setScalable:YES];
  109.                     [image setSize:&newsize];
  110.                     }
  111.                 [image setName:login];
  112.                 }
  113.             else
  114.                 image=[image free];
  115.             }
  116.         NXZoneFree([self zone],login);
  117.         }
  118.     [self setImage:image];
  119.     [self display];
  120.     return image? TRUE : FALSE;
  121.     }
  122.  
  123.  
  124. //-----------------------------------------------------------------------
  125. // DRAWING
  126. //-----------------------------------------------------------------------
  127.  
  128. - drawSelf:(const NXRect *)rects :(int)rectCount
  129.     {
  130.     [super drawSelf:rects:rectCount];
  131.     PSsetgray(NX_LTGRAY);
  132.     NXRectFill(&bounds);
  133.     if(myImage)
  134.         {
  135.         NXRect frects[5];
  136.         float    fgrays[5]={NX_DKGRAY,NX_DKGRAY,NX_DKGRAY,NX_BLACK,NX_BLACK};
  137.         NXPoint imgorigin={1,2};
  138.         NXSize size;
  139.         
  140.         // [TRH] Shrinkwrap rects around image.
  141.         [myImage getSize:&size];
  142.         NXSetRect(&frects[0],3,0,size.width+1,size.height+1);
  143.         NXSetRect(&frects[1],0,1,1,size.height+2);
  144.         NXSetRect(&frects[2],0,size.height+2,size.width+2,1);
  145.         NXSetRect(&frects[3],0,1,size.width+1,1);
  146.         NXSetRect(&frects[4],size.width+1,1,1,size.height+2);
  147.         NXRectFillListWithGrays(frects,fgrays,5);
  148.         [myImage composite:NX_SOVER toPoint:&imgorigin];
  149.  
  150.         }
  151.     else
  152.         {
  153.         NXPoint imgorigin={0,2};
  154.         [[NXImage findImageNamed:"of"] composite:NX_SOVER toPoint:&imgorigin];
  155.         }
  156.     return self;
  157.     }
  158.     
  159.  
  160. //-----------------------------------------------------------------------
  161. // THAT'S IT
  162. //-----------------------------------------------------------------------
  163.  
  164. @end
  165.